package sh.wn36.vehiclemanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import sh.wn36.vehiclemanage.controller.voe.DriverQuery;
import sh.wn36.vehiclemanage.controller.voe.DrivingRecordQuery;
import sh.wn36.vehiclemanage.entity.Car;
import sh.wn36.vehiclemanage.entity.Driver;
import sh.wn36.vehiclemanage.entity.DrivingRecord;
import sh.wn36.vehiclemanage.service.CarService;
import sh.wn36.vehiclemanage.service.DriverService;
import sh.wn36.vehiclemanage.service.DrivingRecordService;
import sh.wn36.vehiclemanage.util.Result;
import sh.wn36.vehiclemanage.vo.driverInfo;
import sh.wn36.vehiclemanage.vo.driverRecordInfo;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@Api(value = "出车信息")
@CrossOrigin
@RestController
@RequestMapping("/driverRecord")
public class DrivingRecordController {

    @ApiModelProperty("成员变量 DrivingRecordService")
    @Autowired
    private DrivingRecordService drivingRecordService;

    @ApiModelProperty("成员变量 CarService")
    @Autowired
    private CarService carService;

    @ApiModelProperty("成员变量 DriverService")
    @Autowired
    private DriverService driverService;


    @PostMapping("/pageListDriverRecord/{current}/{limit}")
    @ApiModelProperty("出车记录的回显")
    public Result pageListDriverRecord(@ApiParam(value = "当前页码",name = "currentPage",required = true)@PathVariable("current")Integer current,
                                       @ApiParam(value = "每页显示记录数",name = "pageSize",required = true)@PathVariable("limit")Integer limit,
                                       @ApiParam(value = "查询条件",name = "driverRecordQuery",required = false)@RequestBody DrivingRecordQuery drivingRecordQuery) {
        Page<driverRecordInfo> page = new Page<>(current,limit);
        QueryWrapper<driverRecordInfo> wrapper = new QueryWrapper<>();
        System.out.println(drivingRecordQuery);
        if(drivingRecordQuery.getCarNo() == null|| ("").equals(drivingRecordQuery.getCarNo())){
            wrapper.orderByDesc("gmt_modified");

            IPage<driverRecordInfo> driverRecord = drivingRecordService.getAll1(page, wrapper);
            List<driverRecordInfo> records = driverRecord.getRecords();
            long total = page.getTotal();



            return Result.OK().data("total",total).data("items",records);
        }else{
            String carNo = drivingRecordQuery.getCarNo();
            if(!StringUtils.isEmpty(carNo)){
                wrapper.like("c.car_no",carNo);
            }
            wrapper.orderByDesc("gmt_modified");
            System.out.println("-----------------");
            IPage<driverRecordInfo> driverRecord = drivingRecordService.getAll2(page, wrapper);
            List<driverRecordInfo> records = driverRecord.getRecords();
            long total = page.getTotal();
            return Result.OK().data("total",total).data("items",records);
        }
    }

    @ApiModelProperty("删除出车信息")
    @RequestMapping("/deleteDriverRecordByIds/{ids}")
    public Result deleteByIds(@PathVariable("ids") int[] ids) {
        for (int i = 0; i < ids.length; i++) {
            int id = ids[i];
            drivingRecordService.removeById(id);
        }
        return Result.OK();
    }

    @ApiModelProperty("查询车辆信息")
    @RequestMapping("/getCar")
    public Result getCar(){

        List<Car> list = carService.getAll2();
        return Result.OK().data("items",list);
    }

    @ApiModelProperty("查询所有车辆信息")
    @RequestMapping("/getAllCar")
    public Result getAllCar(){
        List<Car> list = carService.list(null);
        return Result.OK().data("items",list);
    }

    @ApiModelProperty("查询未出差的驾驶员信息")
    @RequestMapping("/getDriver")
    public Result getDriver(){
        List<Driver> list = driverService.getAll3();
        return Result.OK().data("items",list);
    }

    @ApiModelProperty("查询所有驾驶员信息")
    @RequestMapping("/getAllDriver")
    public Result getAllDriver(){
        List<Driver> list = driverService.list(null);
        return Result.OK().data("items",list);
    }


    @ApiModelProperty("保存出车信息")
    @PostMapping("/saveDriverRecord")
    public Result saveDriverRecord(@RequestBody DrivingRecord drivingRecord, @RequestHeader("X-Token") String token){
        Integer carId = drivingRecord.getCarId();
        Car car = new Car();
        car.setId(carId);
        car.setCarState(36);
        carService.updateById(car);

        //通过登陆名来查出用户的id



        Integer driverId = drivingRecord.getDriverId();
        Driver driver = new Driver();
        driver.setId(driverId);
        driver.setIsdisable(1);
        boolean result = drivingRecordService.save(drivingRecord);
        if (result)
            return Result.OK();
        else
            return Result.Fail();
    }

    @ApiModelProperty("回显修改的信息")
    @GetMapping("/findDriverRecordById/{id}")
    public Result findDriverRecordById(@ApiParam("记录的id")@PathVariable("id") String id){


        DrivingRecord drivingRecord = drivingRecordService.getById(id);
        return Result.OK().data("items",drivingRecord);
    }
    @ApiModelProperty("修改出车记录")
    @PostMapping("/updateDriverRecord")
    public Result updateDriverRecord(@RequestBody DrivingRecord drivingRecord){
        boolean result = drivingRecordService.updateById(drivingRecord);
        if (result)
            return Result.OK();
        else
            return Result.Fail();

    }
    @ApiModelProperty("添加回车记录")
    @PostMapping("/updateDriverRecordBack")
    public Result updateDriverRecordBack(@RequestBody DrivingRecord drivingRecord){
        Integer carId = drivingRecord.getCarId();
        Car car = new Car();
        car.setId(carId);
        car.setCarState(38);
        carService.updateById(car);

        Integer driverId = drivingRecord.getDriverId();
        Driver driver = new Driver();
        driver.setId(driverId);
        driver.setIsdisable(0);
        driverService.updateById(driver);

        boolean result = drivingRecordService.updateById(drivingRecord);
        if (result)
            return Result.OK();
        else
            return Result.Fail();
    }

}

